﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Collections;
using Supersprintz;

namespace SuperSprintZ
{
    class Menu : IDrawableGameObject
    {
        private int x, y;
        private List<MenuText> menuItems;
        private int selection; //active menu element, basically an index for the menuItems list
        private SpriteFont font;
        private KeyboardState keyboardState;
        private KeyboardState prevKeyboardState;
        Game1 game;
        private bool bDrawHelp;
        private Menu helpMenu;
        private String helptext;
        private Vector2 helppos;
        
        public Menu(Game1 game, SpriteFont font, int resx, int resy)
        {
            menuItems = new List<MenuText>();
            selection = 0;   
            this.x = resx;
            this.y = resy;
            this.font = font;
            this.game = game;
            bDrawHelp = false;
            makeHelpMenu();
        }

        public void makeHelpMenu()
        {
            helppos = new Vector2(50, 50);
            helptext =  "Player 1 uses arrow keys to move his/her car\n" +
                        "Player 2 uses WASD keys to move his/her car\n\n" +
                        "Up/W key accelerates, down/S key brakes.\n" +
                        "Right/D and left/A keys turn the car accordingly\n" +
                        "\n" +
                        "Win the race by finishing the track first\n"+"Exit by pressing enter";
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (bDrawHelp)
            {
                spriteBatch.DrawString(font, helptext, helppos, Color.White);
            }
            else
            {
                foreach (MenuText item in menuItems)
                {
                    spriteBatch.DrawString(font, item.text, item.position, item.getColour());
                }
            }
        }

        private void changeSelection(int amount)
        {
            menuItems[selection].bActiveSelection = false;
            int newpos = selection + amount;
            if (newpos < 0)
                newpos = menuItems.Count - 1;
            if (newpos == menuItems.Count)
                newpos = 0;
            selection = newpos;
            
            menuItems[selection].bActiveSelection = true;
            
        }

        public void setFont(SpriteFont font)
        {
            this.font = font;
        }

        public void Update(GameTime gameTime)
        {
            changeSelection(0);
            prevKeyboardState = keyboardState;
            keyboardState = Keyboard.GetState();
            if (keyboardState == prevKeyboardState)
                return;
            if (!bDrawHelp)
            {
                if (keyboardState.IsKeyDown(Keys.Up))
                    changeSelection(-1);
                if (keyboardState.IsKeyDown(Keys.Down))
                    changeSelection(1);
            }
            if (keyboardState.IsKeyDown(Keys.Enter))
                handleEnter();
            if(keyboardState.IsKeyDown(Keys.Escape))
            {
                if(bDrawHelp)
                    bDrawHelp = false;
            }
                

        }

        private void handleEnter()
        {
            if (!bDrawHelp)
            {
                if (selection == 0)
                {
                    game.reLoad(game.ratanro);
                    game.bMainmenu = false;
                }
                if (selection == 1)
                {
                    game.ratanro = 0;
                }
                if (selection == 2)
                {
                    game.ratanro = 1;
                }
                if (selection == 3)
                {
                    bDrawHelp = true;
                }
                if (selection == 4)
                {
                    game.Exit();
                }
            }
            else
            {
                bDrawHelp = false;
            }
        }

        public void addText(String text, int x, int y)
        {
            menuItems.Add(new MenuText(text, x, y));
        }
    }
}
